﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using Recon.Portal.DataObjects;
using Recon.Portal.Business;

namespace Recon.Portal.WebUI
{
    public partial class SearchDiscount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                control.Value = Request.QueryString["textbox"].ToString();
                path.Value = Request.QueryString["PageRequest"].ToString();
                BindGrid();
                txtDisc.Focus();

            }
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvDisc.PageIndex = 0;
            gvDisc.DataSource = null;
            gvDisc.DataBind();
            BindGrid();
        }

        protected void gvDisc_SelectedIndexChanged(object sender, EventArgs e)
        {
            string remitterid;
            remitterid = ((LinkButton)gvDisc.SelectedRow.FindControl("lbtnDisc_Code")).Text;
            string strScript = "<script>";
            strScript += "window.opener.document.all('ctl00$BodyHolder$txtDiscountcode').value = '" + remitterid + "';";
            strScript += "self.close();</script>";
            Response.Write(strScript);
        }

        #region User Defined Function

        public void BindGrid()
        {
            DiscountPenaltyEntitiy discPenEntity = new DiscountPenaltyEntitiy();
            List<DiscountPenalty> discPenLst = null;
            try
            {
                if (txtDisc.Text.Trim() == "")
                {
                    discPenLst = discPenEntity.GetDiscountDetails(Convert.ToString(control.Value).Trim(), ((User)Session["CurrentUserVO"]).CustomerID);
                }
                else
                {
                    discPenLst = discPenEntity.GetDiscountDetails(Convert.ToString(txtDisc.Text).Trim(), ((User)Session["CurrentUserVO"]).CustomerID);
                }

                // if (remit == null)
                if (discPenLst == null || discPenLst.Count <= 0)
                {
                    lblDisccnt.Text = "No Records Found";
                }
                else
                {
                    gvDisc.DataSource = discPenLst;
                    gvDisc.DataBind();
                    lblDisccnt.Text = ((List<DiscountPenalty>)gvDisc.DataSource).Count.ToString() + " records found.";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        protected void gvDisc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //double value = 0.00;
                //if (e.Row.Cells[4].Text != "" && e.Row.Cells[4].Text != "&nbsp;")
                //{
                //    value = Convert.ToDouble(e.Row.Cells[4].Text);
                //    e.Row.Cells[4].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
                //    e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
                //}
                //else
                //{
                //    e.Row.Cells[4].Text = "0.00";
                //}
                DateTime dt;
                if (e.Row.Cells[1].Text != "" && e.Row.Cells[1].Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(e.Row.Cells[1].Text);
                    e.Row.Cells[1].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[1].Text = "";
                }
            }
        }
    }
}